package danny.hoi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.export.cargo.ContractDao;
import danny.hoi.dao.export.cargo.ExtCproductDao;
import danny.hoi.domain.export.cargo.Contract;
import danny.hoi.domain.export.cargo.ExtCproduct;
import danny.hoi.domain.export.cargo.ExtCproductExample;
import danny.hoi.service.cargo.ExtCproductService;
import danny.hoi.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public List<ExtCproduct> findAll(ExtCproductExample example) {
        return extCproductDao.selectByExample(example);
    }

    @Override
    public PageInfo<ExtCproduct> findByPages(ExtCproductExample example, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);

        return new PageInfo<>(extCproducts);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(ExtCproduct extCproduct) {
        extCproduct.setId(UuidUtil.getUuid());
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        Double amout = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amout = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amout);
        contract.setTotalAmount(contract.getTotalAmount() + amout);
        contract.setExtNum(contract.getExtNum() + 1);
        //更新contract
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        //计算修改后的货物金额
        Double amount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);
        //查出原附件
        ExtCproduct cproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Contract contract =
                contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //更新购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - cproduct.getAmount() + amount);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    @Override
    public void delete(String id) {
        //附件
        ExtCproduct dbEcp = extCproductDao.selectByPrimaryKey(id);
        //购销合同
        Contract contract = contractDao.selectByPrimaryKey(dbEcp.getContractId());
        //减去附件总金额
        contract.setTotalAmount(contract.getTotalAmount() - dbEcp.getAmount());
        //附件数 - 1
        contract.setExtNum(contract.getExtNum() - 1);
        //更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);
        //删除
        extCproductDao.deleteByPrimaryKey(id);
    }
}
